package isp.apps.example.shoppingcart.persistence.hibernate;

import isp.apps.example.shoppingcart.domain.Item;
import isp.apps.example.shoppingcart.persistence.ItemDirectory;
import isp.apps.example.shoppingcart.persistence.ItemRepository;
import isp.apps.example.shoppingcart.persistence.PersistenceManagerFactory;

import isp.utils.cjis.persistence.PersistenceException;
import isp.utils.cjis.persistence.PersistenceManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HibernateItemRepository
  implements ItemRepository
{

  private PersistenceManager persistenceManager =
    PersistenceManagerFactory.getPersistenceManager();

  private static final Log log =
    LogFactory.getLog(HibernateCartItemsRepository.class);

  public HibernateItemRepository()
  {

  }

  public void saveItem(Item i)
  {
    try
    {
      if (i.getId() == null)
        persistenceManager.save(i);
      else
        persistenceManager.update(i);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
    }
  }

  public void deleteItem(Item i)
  {
    try
    {
      persistenceManager.delete(i);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
    }
  }
}
